/* eslint-disable */
import { hiprint } from 'vue-plugin-hiprint';

let Modal = [];
let printData = {};
let jadeCount = 0;

const rules = {
    status: "审核状态",
    auditedBy: "调整人员",
    createTime: "创建时间",
    auditTime: "调整时间",
    remark: "备注",
    id: "调整单id",
    barcode: "条码",
    costPrice: "成本",
    tagPrice: "标价",
    marketPrice: "市场价",
    jadeName: "货品名",
    oriNum: "原数量",
    newNum: "调整后数量",
   
};

export { printData };

export function generateDynamicStructure(options) {
    const fields = [];
    const columns = [];

    for (const [field, text] of Object.entries(options)) {
        fields.push({ text, field });
        columns.push({ title: text, align: 'center', field, width: 100 });
    }

    return { fields, columns: [columns] };
}

export function creatModal(options) {
    var arr = [];
    // 塞两个辅助模板
    arr.push(
        new hiprint.PrintElementTypeGroup("辅助", [
            {
                tid: 'order.hline',
                title: '横线',
                type: 'hline'
            },
            {
                tid: 'order.vline',
                title: '竖线',
                type: 'vline'
            },
            {
                tid: 'order.rect',
                title: '矩形',
                type: 'rect'
            },
            {
                tid: 'order.oval',
                title: '椭圆',
                type: 'oval'
            }
        ])
    );
    arr.push(
        new hiprint.PrintElementTypeGroup("其他", [
            { tid: 'order.customText', title: '文本', type: 'text' },
            {
                tid: 'order.longText', title: '长文本', type: 'longText', options: {
                    width: 200,
                    testData: '长文本分页/不分页测试'
                },
            }
        ])
    );

    // 塞价格调整表格
    var table = [];
    const { fields, columns } = generateDynamicStructure(rules);

    table.push({
        tid: 'auditPrice.table',
        title: '价格调整数据表格',
        type: 'table',
        options: {
            field: 'table',
            fields: fields,
        },
        editable: true,
        columnDisplayEditable: true,
        columnDisplayIndexEditable: true,
        columnTitleEditable: true,
        columnResizable: true,
        columnAlignEditable: true,
        columns: columns,
    });

    arr.push(new hiprint.PrintElementTypeGroup("表格数据", table));

    arr.push(...options);
    return arr;
}

// 处理传入的参数动态生成设计元素
export function createElementTypes(rules, options) {
    printData = {};
    jadeCount = 0;
    // count用来命名模板名字一起区别field，每行数据的模板名不一样，每个模板内的设计元素的field要区别
    let count = 1;
    // 这个用来放所有模板
    let allModalArray = [];
    // 表格数组
    let tableArray = [];
    options.forEach(opt => {
        // 进行深拷贝,好像也没必要(确实没必要，如果数据不是从参数那拿而是从某个js文件里拿的就需要，不能污染原js数据，但不管了保留也没事)
        let item = JSON.parse(JSON.stringify(opt));
        // 模板数组
        let modalArray = [];
        // 表格数组内的对象元素，具体你看看print-data.js格式就知道我为什么要这么写
        let tableElement = {};
        // 将入库单的属性进行变成设计元素格式
        for(const key in item){
            if (!Array.isArray(item[key]) && item[key]!== null && key in rules) {
                const translatedTitle = rules[key] || key;
                printData[`${key}${count}`] = item[key];
                tableElement[`${key}`] = item[key];
                modalArray.push({
                    tid: `order.${key}${count}`,
                    title: translatedTitle,
                    data: translatedTitle,
                    type: 'text',
                    options: {
                        field: `${key}${count}`,
                        testData: translatedTitle,
                        height: 17,
                        fontSize: 16.5,
                        fontWeight: "700",
                        textAlign: "center",
                        hideTitle: true
                    }
                });
                if (/code|Code/.test(key)) {
                    modalArray.push({
                        tid: `order.${key}${count}.qrcode`,
                        text: `${translatedTitle}二维码`,
                        type: 'text',
                        options: {
                            field: `${key}${count}`,
                            testData: "XS888888888",
                            height: 17,
                            fontSize: 16.5,
                            fontWeight: "700",
                            textAlign: "center",
                            textType: "qrcode"
                        }
                    });
                    modalArray.push({
                        tid: `order.${key}${count}.barcode`,
                        text: `${translatedTitle}条形码`,
                        type: 'text',
                        options: {
                            field: `${key}${count}`,
                            testData: "XS888888888",
                            height: 17,
                            fontSize: 16.5,
                            fontWeight: "700",
                            textAlign: "center",
                            textType: "barcode"
                        }
                    });
                }
            }
        }
        // 把表格数据填充
        tableArray.push(tableElement);
        // 把处理好的模板塞到数组
        allModalArray.push(
            new hiprint.PrintElementTypeGroup(`价格调整单${count}`, modalArray)
        );
        count++;
    });
    printData['table'] = tableArray;
    Modal = creatModal(allModalArray);
}

// 自定义设计元素1
export const Provider = function (ops) {
    var addElementTypes = function (context) {
        context.removePrintElementTypes("auditPriceModule");
        context.addPrintElementTypes(
            "auditPriceModule", Modal
        );
    };
    return {
        addElementTypes: addElementTypes
    };
};
export default [{
    name: 'inbound.auditPrice',
    value: 'auditPriceModule',
    type: 1,
    f: Provider()
}];